package com.corner.ui.activity;

import android.view.View;
import android.widget.EditText;

import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.presenter.imp.ChangePasswordPresenter;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.view.IChangePasswordView;
import com.corner.utils.AppManager;
import com.corner.utils.SoftInputUtil;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.MainActionBar;

/**
 * Created by Administrator on 2015/5/23.
 */
public class ChangePasswordActivity extends BaseActivity<ChangePasswordPresenter> implements View.OnClickListener, IChangePasswordView {
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_change_password_oldpassword)
    private EditText etOldPassword;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_change_password_newpassword)
    private EditText etNewPassword;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_change_password_rewrite_password)
    private EditText etReNewPassword;

    private MainActionBar actionBar;


    @Override
    protected ChangePasswordPresenter createP() {
        return new ChangePasswordPresenter();
    }

    @Override
    protected void setOnClick() {
        actionBar.btnLeft.setOnClickListener(this);
        actionBar.btnRight.setOnClickListener(this);
    }

    @Override
    protected void initData() {
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_change_password);
    }

    @Override
    public void initView() {
        super.initView();
        actionBar = new MainActionBar(this);
        actionBar.initActionBarButton();
        actionBar.btnLeft.setVisibility(View.VISIBLE);
        actionBar.btnRight.setVisibility(View.VISIBLE);
        actionBar.btnLeft.setText(getString(R.string.go_back));
        actionBar.btnRight.setText(getString(R.string.change_password_action_bar_right_ensure));
        actionBar.tvMiddle.setText(getString(R.string.change_password_action_bar_left_notice));
        actionBar.ivRight.setVisibility(View.GONE);
        SoftInputUtil.openSoftInput(this,etOldPassword);
    }

    @Override
    public void onClick(View v) {
        if (v == actionBar.btnLeft) {
            AppManager.getInstance().finishActivity(this);
        } else if (v == actionBar.btnRight) {
            if (checkPassword(etOldPassword.getText()) && checkPassword(etNewPassword.getText()) && checkPassword(etReNewPassword.getText())) {
                if (etNewPassword.getText().toString().equals(etReNewPassword.getText().toString())) {
                    excuteTaskWhenNetOk(new Task() {
                        @Override
                        public void onExcute() {
                            presenter.changePassword(etOldPassword.getText().toString(), etNewPassword.getText().toString());
                        }
                    });
                } else {
                    ToastUtil.showCustomToastInRelease(this, getString(R.string.change_password_rewrite_error));
                }
            } else {
                ToastUtil.showCustomToastInRelease(this,
                        R.string.login_password_null_notice);
            }
        }
    }

    /**
     * 校验密码
     *
     * @param s
     * @return
     */
    private boolean checkPassword(CharSequence s) {
        return s.length() != 0;
    }

    @Override
    public void changePasswordSucess() {
        ToastUtil.showCustomToastInRelease(ChangePasswordActivity.this, getString(R.string.change_password_sucess));
        AppManager.getInstance().finishActivity(this);
    }

    @Override
    public void changePasswordError(String message) {
        ToastUtil.showCustomToastInRelease(ChangePasswordActivity.this, message);
    }
}
